/**
 * This source code is property of Ideyatech, Inc.
 * All rights reserved. 
 *
**/

/**
* 
* Module that handles the drag and drop of Case Actions.
* 
* @requires jQuery($), jQuery UI & sortable/draggable UI modules
* @class caseActionMaster
* @namespace IDEYATECH.caseActionMaster
**/
IDEYATECH.caseActionMaster = function() {
	/*Default url to call in persisting our case action order*/
	var _urlOrder = "case-action-persist.jspx?methodName=saveCaseActionOrder&order=";

	return {		
		/**
		 * Method that saves the order of case actions.
		 * 
		 * @param event - the event that was caught
		 * @param el - the element that triggered the event
		 * 
		 */
		executeOrderUpdate: function(event, el){
			if(event.target == el){
            	var object = el;
    			var caseActionArrParameter;
    			setTimeout(function() {
    				caseActionArrParameter = iNettuts.getCaseActionArrangement(object);
    				$.ajax({
    					type: "POST",
    					url: IDEYATECH.caseActionMaster.getUrlOrder() + caseActionArrParameter
    				});
    			}, 500);	
            }
		},
		
		/**
		* Getter method for _urlOrder
		*/
		getUrlOrder: function() {
			return _urlOrder;
		},
		/**
		* Setter method for _urlOrder
		*/
		setUrlOrder: function(sUrl) {
			_urlOrder = sUrl;
		},
				
	};//end return
}();

/**
 * @author Script from NETTUTS.com [by James Padolsey], modified by Ideyatech, Inc.
 * Module that builds the Case Action Master.
 * @requires jQuery($), jQuery UI & sortable/draggable UI modules, jquery-ui-personalized-1.6rc2.min.js
 */
var iNettuts = {
    
    jQuery : $,
    
    settings : {
        columns : '.column',
        caseActionSelector: '.case-action-li',
        handleSelector: '.case-action-content',
        caseActionDefault : {
            movable: true
        },
        caseActionIndividual : {
            intro : {
                movable: false
            }
        }
    },

    init : function () {
        this.makeSortable();
    },
    
    getCaseActionSettings : function (id) {
        var $ = this.jQuery, settings = this.settings;
        return (id && settings.caseActionIndividual[id]) ? $.extend({},settings.caseActionDefault,settings.caseActionIndividual[id]) : settings.caseActionDefault;
    },
    
    getCaseActionArrangement : function (object) {
    	var arrangement = "";
        var rows = $(object).delay(800).parents('ul').each(function(){
        	$(this).find(".case-action-content").parent().each(function(){
				arrangement += $(this).attr('id') + ",";
			});
			arrangement = arrangement.substring(0, arrangement.length - 1);
		});
       
        return arrangement;       
    },
    
    makeSortable : function () {
        var iNettuts = this,
            $ = this.jQuery,
            settings = this.settings;
            
            $sortableItems = (function () {
                var notSortable = '';
                $(settings.caseActionSelector, $(settings.columns)).each(function (i) {
                    if (!iNettuts.getCaseActionSettings(this.id).movable) {
                    	if(!this.id) {
                            this.id = 'case-action-no-id-' + i;
                        }
                        notSortable += '#' + this.id + ',';
                    }
                });
                return $('> li:not(' + notSortable + ')', settings.columns);
            })();
        
	        $sortableItems.find(settings.handleSelector).contents().andSelf().css({
	            cursor: 'move'
	        }).mousedown(function (e) {
	            $sortableItems.css({width:''});
	            $(this).parent().css({
	                width: $(this).parent().width() + 'px'
	            });
	        }).mouseup(function (e) {
	            if(!$(this).parent().hasClass('dragging')) {
	            	$(this).parent().css({
	                    width: $(this).parent().width() + 'px'
	                });
	            } else {
	                $(settings.columns).sortable('disable');
	            }
	            IDEYATECH.caseActionMaster.executeOrderUpdate(e, this);            
	        });
	
	        $(settings.columns).sortable({
	            items: $sortableItems,
	            connectWith: $(settings.columns),
	            handle: settings.handleSelector,
	            placeholder: 'case-action-placeholder',
	            forcePlaceholderSize: true,
	            revert: 300,
	            delay: 100,
	            distance: 30,
	            opacity: 0.8,
	            containment: 'document',
	            start: function (e,ui) {
	                $(ui.helper).addClass('dragging');
	            },
	            stop: function (e,ui) {
	                $(ui.item).css({width:''}).removeClass('dragging');
	                $(settings.columns).sortable('enable');
	            }
	        }).disableSelection();
    },
    
    disableSortable : function() {
    	var iNettuts = this,
        $ = this.jQuery,
        settings = this.settings;
        
        $sortableItems = (function () {
            var notSortable = '';
            $(settings.caseActionSelector, $(settings.columns)).each(function (i) {
                if (!iNettuts.getCaseActionSettings(this.id).movable) {
                	if(!this.id) {
                        this.id = 'case-action-no-id-' + i;
                    }
                    notSortable += '#' + this.id + ',';
                }
            });
            return $('> li:not(' + notSortable + ')', settings.columns);
        })();
  
        $(settings.columns).sortable({ disabled: true });
    }
    
};